package com.dongway.netcondition.socketToBox;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import com.dongway.netcondition.model.DevInfo;

public class GetIpPhone {

    public static DevInfo getIpPhone(Integer dev_id) {

        DevInfo devInfo = new DevInfo();
        devInfo.setId(dev_id);
        // 根据设备ＩＤ获取Socket
        Socket socket = SocketContent.getInstance().getSocket(dev_id);
        // Map<String, String> map = new HashMap<String, String>();
        // InputStream in = null;
        String hex = "";
        // 发送报文指令头
        byte[] b1 =
                { 0x61, 0x31, 0x30, 0x2c, (byte) 0x88, (byte) 0x87, 0x03, 0x06, 0x02, 0x00, 0x00,
                 0x00 };

        // 设置ID来查询
        hex = Integer.toHexString(dev_id);
        hex = hex.length() > 2 ? hex : ("0" + hex);
        byte[] b2 = HexStringToBytes.hexStringToBytes(hex);
        b1[10] = b2[0];

        try {
            OutputStream out = socket.getOutputStream();
            out.write(b1);
            out.flush();
        } catch (IOException e) {
            System.out.println("上报信息查询失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }
        SocketInfo info = SocketContent.getInfo(dev_id);
        while (info.getIpPhone() == null) {
            System.out.println("上报信息查询中。。。。。。");
            // 为空，一直等！
            try {
				Thread.sleep(1000);
			} catch (InterruptedException ige) {
			}
        }

        // 等待ID线程获取数据
        byte[] b = info.getIpPhone();
        String str = "";
        int len = b.length;

        // out.write(b1);
        // out.flush();
        //
        // // 接收数据
        // in = socket.getInputStream();
        // byte[] b = new byte[1024];
        // String str = "";
        // int len = in.read(b);
        //
        // //对接收的数据进行预处理，若不是查询的继续等待。
        // while(len!=224)
        // {
        // out.write(b1);
        // out.flush();
        // // 刷新
        // in = socket.getInputStream();
        // b = new byte[1024];
        // len = in.read(b);
        //
        // }
        //

        // 遍历获取的查询信息
        char[] ch = new char[b.length];
        int[] inf = new int[len];
        for (int i = 0, j = 0; i < len; i++) {

            inf[i] = (int) b[i];
            if (inf[i] < 0)
                inf[i] += 256;

            // ipA-a0
            if (19 < i && i < 24) {
                if (i == 23) {
                    str += inf[i];
                    devInfo.setPnServerIp(str);
                } else
                    str += inf[i] + ":";
            }

            // ipA-a1
            if (25 < i && i < 30) {
                if (i == 26)
                    str = "";
                if (i == 29) {
                    str += inf[i];
                    devInfo.setPnLocalIp(str);
                } else
                    str += inf[i] + ":";
            }
            // ipB-b0
            if (31 < i && i < 36) {
                if (i == 32)
                    str = "";
                if (i == 35) {
                    str += inf[i];
                    devInfo.setVpnServerIp(str);
                } else
                    str += inf[i] + ":";
            }
            // ipB-b1
            if (37 < i && i < 42) {
                if (i == 38)
                    str = "";
                if (i == 41) {
                    str += inf[i];
                    devInfo.setVpnLocalIp(str);;
                } else
                    str += inf[i] + ":";
            }

            // 有线上报地址
            if (49 < i && i < 54) {
                if (i == 50)
                    str = "";
                if (i == 53) {
                    str += inf[i];
                    devInfo.setWiredReportIp(str);
                } else
                    str += inf[i] + ":";
            }

            if (53 < i && i < 58) {
                if (i == 54)
                    str = "";
                if (i == 57) {
                    str += inf[i];
                    devInfo.setWirelessReportIp(str);
                } else
                    str += inf[i] + ":";
            }

            if (57 < i && i < 69) {
                if (i == 58)
                    str = "";
                if (i == 68) {
                    str += inf[i] - 48;
                    devInfo.setPhoneNum(str);
                } else
                    str += inf[i] - 48;
            }

            if (i > 83 && i < b.length) {

                ch[j] = (char) inf[i];
                j++;
                i = i + 1;
            }

        }
        str = new String(ch);
        devInfo.setSmsContent(str);
        // 有限上报端口
        str = Integer.toHexString(inf[13]) + Integer.toHexString(inf[12]);
        int port = Integer.parseInt(str, 16);
        str = port + "";

        // 无线上报端口
        str = Integer.toHexString(inf[15]) + Integer.toHexString(inf[14]);
        port = Integer.parseInt(str, 16);
        str = port + "";
       
        return devInfo;
    }

}
